<?php

function alamat_karyawan($karyawan_id){
	$header  = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'), 
		'Type'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Alamat'=>array('style'=>'border-bottom:2px solid;width:35%'),  
		'Kota'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Propinsi'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Kodepos'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'),   
	);
	$query = "
		SELECT * FROM alamat a
			INNER JOIN kodepos b ON a.kodepos_id = b.kodepos_id
				WHERE a.karyawan_id = {$karyawan_id} 
		";
	$result = my_query($query);
	while( $r = my_fetch_array($result ) ){
		$ro = my_get_data_by_id('kodepos','kodepos_id',$r['kodepos_id']);
 
		$i++;
		
		$editproperty = array(
						'href'=>'index.php?com=alamat&kid='.$_GET['id'].'&id='.$r['alamat_id'], 
						'title'=>'Edit',
						'rel'=>'facebox'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$deleteproperty = array(
						'href'=>'javascript:void();', 
						'title'=>'Edit',
						'onclick'=>'javascript:confirmDelete( \'remove_alamat\','.$r['alamat_id'].')'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		$row[] = array(
			'#'=> $i,  
			'type'=>    strtoupper($r['type'])   ,  
			'alamat'=>    $r['alamat'] .', '.$r['kelurahan'] .', '.$r['kecamatan'] ,  
			'kota'=>   $ro['kota'] ,  
			'propinsi'=>  ucfirst($ro['propinsi']),  
			'kodepos'=>  ucfirst($ro['kodepos']),  
			'edit'=>position_text_align(  $edit_button .' '.$delete_button,'right')
		); 
	
	}
	
	if($i < 5){
		for($ycc=$i; $ycc<=4; $ycc++){
		$row[] = array(
			'#'=> '&nbsp;',  
			'nama'=>  '&nbsp;', 
			's'=>  '&nbsp;', 
			'f'=>  '&nbsp;', 
			'af'=>  '&nbsp;', 
			'fs'=>  '&nbsp;', 
			'sas'=>  '&nbsp;' 
		); 
		}
	}
	$datascc = table_rows_plain($row ); 
	$eeee  = table_builder_plain($header  , $datascc ,  5,false ); 
	return $eeee. '<div style="text-align:right;width:100%;font:12px verdana;">'.admin_link('<a href="index.php?com=alamat&kid='.$_GET['id'].'&task=edit_popup_spesifikasi&id=0" rel="facebox">[Tambah data]</a>' ).'</div>'; 
	 
}


function alamat_edit($id){ 
my_set_file_js(array(
		'components/system/jquery/autocomplete/jquery.autocomplete.js' ,
		'components/system/js/calendar/calendarDateInput.js'));	 

my_set_file_css(

array(
		'components/system/jquery/autocomplete/jquery.autocomplete.css' 
	)
);

my_set_code_js('  
function findValue(li) {
	if( li == null ) return alert("No match!"); 
	if( !!li.extra ) var sValue = li.extra[0]; 
	else var sValue = li.selectValue;
}

function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	return   row[0];
}

$(document).ready(function() {
	$("#propinsi").autocomplete(
		"autocomplete_daftar_propinsi.php",
		{
			delay:10,
			minChars:2,
			matchSubset:1,
			matchContains:1,
			cacheLength:5,
			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:true
		}
	);
	 
});

function findValueKota(li) {
	if( li == null ) return alert("No match!"); 
	if( !!li.extra ) var sValue = li.extra[0]; 
	else var sValue = li.selectValue;
}

function selectItemKota(li) {
	findValueKota(li);
}

function formatItemKota(row) {
	return   row[0];
}  
 
 
	
$(document).ready(function() {
	$("#kota").autocomplete(
		"autocomplete_daftar_kota.php",
		{
			delay:200,
			minChars:2,
			matchSubset:1,
			matchContains:1,
			cacheLength:5,
			onItemSelect:selectItemKota,
			onFindValue:findValueKota,
			formatItem:formatItemKota,  
			autoFill:true,
			extraParams: {propinsi: function(){ return $("#propinsi").val(); } }
		}
	);
	 
});

function findValueKecamatan(li) {
	if( li == null ) return alert("No match!"); 
	if( !!li.extra ) var sValue = li.extra[0]; 
	else var sValue = li.selectValue;
}

function selectItemKecamatan(li) {
	findValueKota(li);
}

function formatItemKecamatan(row) {
	return   row[0];
} 

$(document).ready(function() {
	$("#kecamatan").autocomplete(
		"autocomplete_daftar_kecamatan.php",
		{
			delay:10,
			minChars:2,
			matchSubset:1,
			matchContains:1,
			cacheLength:5,
			onItemSelect:selectItemKecamatan,
			onFindValue:findValueKecamatan,
			formatItem:formatItemKecamatan, 
			autoFill:true,
			extraParams: {kota: function() { return $("#kota").val(); } }
		}
	);
	 
});

function findValueKelurahan(li) {
	if( li == null ) return alert("No match!"); 
	if( !!li.extra ) var sValue = li.extra[0]; 
	else var sValue = li.selectValue;
}

function selectItemKelurahan(li) {
	findValueKota(li);
}

function formatItemKelurahan(row) {
	return   row[0];
} 

$(document).ready(function() {
	$("#kelurahan").autocomplete(
		"autocomplete_daftar_kelurahan.php",
		{
			delay:10,
			minChars:2,
			matchSubset:1,
			matchContains:1,
			cacheLength:5,
			onItemSelect:selectItemKelurahan,
			onFindValue:findValueKelurahan,
			formatItem:formatItemKelurahan, 
			autoFill:true,
			extraParams: {kecamatan: function(){ return $("#kecamatan").val(); } }
		}
	);
	 
});
function findValueKodePos(li) {
	if( li == null ) return alert("No match!"); 
	if( !!li.extra ) var sValue = li.extra[0]; 
	else var sValue = li.selectValue;
}

function selectItemKodePos(li) {
	findValueKota(li);
}

function formatItemKodePos(row) {
	return   row[0];
} 

$(document).ready(function() {
	$("#kodepos").autocomplete(
		"autocomplete_daftar_kodepos.php",
		{
			delay:10,
			minChars:2,
			matchSubset:1,
			matchContains:1,
			cacheLength:5,
			onItemSelect:selectItemKodePos,
			onFindValue:findValueKodePos,
			formatItem:formatItemKodePos, 
			autoFill:true,
			extraParams: {kelurahan: function(){ return $("#kelurahan").val(); } }
		}
	);
	 
});
');
	$fields =my_get_data_by_id('alamat','alamat_id',$id);
	$view = form_header( "alamat karyawan" , "alamat"  );
	$type_datas = array(
			'KTP'=>'KTP',
			'tinggal'=>'TINGGAL',
			//'kos'=>'KOS',
		//	'mes'=>'MESS',
			'Emergency'=>'EMERGENCY'
		);
	$type = array(
		'name'=>'type',
		'value'=>( isset($_POST['type'] ) ? $_POST['type']  : $fields['type']  ),
		'id'=>'type'  
	);
	$form_field_type = form_dropdown($type , $type_datas);
	$view .= form_field_display( $form_field_type   , "Type"    );
	

	$nama = array(
		'name' => 'nama',
		'value' => ( isset($_POST['nama']) ? $_POST['nama'] : $fields['nama'] ),
		'id' => 'nama',
		'size'=>'55'
	);
	$form_field_nama = form_dynamic($nama);
	$view .= form_field_display( $form_field_nama   , "Nama kontak"    );
	
	$nophone = array(
		'name' => 'telepon',
		'value' => ( isset($_POST['telepon']) ? $_POST['telepon'] : $fields['telepon'] ),
		'id' => 'telepon',
		'size'=>'55'
	);
	$form_field_nama = form_dynamic($nophone);
	$view .= form_field_display( $form_field_nama   , "Telepon"    );
	
	$alamat = array(
		'name'=>'alamat',
		'value'=>( isset($_POST['alamat'] ) ? $_POST['alamat']  : $fields['alamat']  ),
		'id'=>'alamat', 
		'cols'=>'45',
		'rows'=>'4'
	);
	$form_field_alamat = form_textarea($alamat);
	$view .= form_field_display( $form_field_alamat   , "Alamat"    );
	 
	$kodepos_data = my_get_data_by_id('kodepos','kodepos_id',( $fields ? $fields['kodepos_id'] : 0 ) );
		$line =	'____________________________';
	$propinsi = array(
		'id' => 'propinsi',
		'name' => 'propinsi',
		'value' => ( isset($_POST['propinsi']) ? $_POST['propinsi'] : strtoupper( $kodepos_data['propinsi'] )),
		'size'	=>'45'
	);
	$form_field_propinsi = form_dynamic($propinsi); 
//	$form_field_propinsi ='<span id="propinsi">'.($kodepos_data ? $kodepos_data['propinsi'] : $line ).'</span>';
	$view .= form_field_display( $form_field_propinsi   , "Propinsi"    );
	$kota = array(
		'id' => 'kota',
		'name' => 'kota',
		'value' => ( isset($_POST['kota']) ? $_POST['kota'] : strtoupper( $kodepos_data['kota']) ),
		'size'	=>'45'
	);
	$form_field_kota = form_dynamic($kota);
	//$form_field_kota ='<span id="kota">'.($kodepos_data ? $kodepos_data['kota'] : $line ).'</span>';
	$view .= form_field_display( $form_field_kota   , "Kota"    );
	$kecamatan = array(
		'name' => 'kecamatan',
		'id' => 'kecamatan',
		'value' => ( isset($_POST['kecamatan']) ? $_POST['kecamatan'] : strtoupper( $kodepos_data['kecamatan'] )),
		'size'	=>'45'
	);
	$form_field_kecamatan = form_dynamic($kecamatan);
	//$form_field_kecamatan ='<span id="kecamatan">'.($kodepos_data ? $kodepos_data['kecamatan'] : $line ).'</span>';
	$view .= form_field_display( $form_field_kecamatan   , "Kecamatan"    );
	
	$kelurahan = array(
		'name' => 'kelurahan',
		'id' => 'kelurahan',
		'value' => ( isset($_POST['kelurahan']) ? $_POST['kelurahan'] : strtoupper( $kodepos_data['kelurahan'] )),
		'size'	=>'35'
	);
	$form_field_kelurahan = form_dynamic($kelurahan);
	//$form_field_kelurahan ='<span id="kelurahan">'.($kodepos_data ? $kodepos_data['kelurahan'] : $line ).'</span>';
	$view .= form_field_display( $form_field_kelurahan   , "Kelurahan"    );
	$kodepos = array(
		'name' => 'kodepos',
		'value' => ( isset($_POST['kodepos']) ? $_POST['kodepos'] : $kodepos_data['kodepos'] ),
		'id' => 'kodepos',
		'size'=>'25',
	//	'onchange'=>'javascript:checkKodePos(this.value)' 
	);
	$form_field_kodepos = form_dynamic($kodepos);
	$view .= form_field_display( $form_field_kodepos   , "Kodepos"    );
	
	
	
	
	
	$catatan = array(
		'name' => 'catatan',
		'value' => ( isset($_POST['catatan']) ? $_POST['catatan'] : $fields['catatan'] ),
		);
	$form_field_catatan = form_textarea($catatan);
	$view .= form_field_display( $form_field_catatan  , "Catatan" );
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 

function form_alamat_submit($karyawan_id, $id = 0){
	 
	$kodepos_id = get_kodepos_id_by_kodepos($_POST['kodepos']);
	$datas = array(
		'alamat' 		=> my_type_data_str($_POST['alamat']),
		'telepon' 		=> my_type_data_str($_POST['telepon']),
		'karyawan_id' 	=> my_type_data_int($_GET['kid']),
		'type' 			=> my_type_data_str($_POST['type']),
		'nama' 			=> my_type_data_str($_POST['nama']),
		'catatan' 		=> my_type_data_str($_POST['catatan']),
		'kodepos_id' 	=> my_type_data_int($kodepos_id),
	);
	if( $id > 0 ){
		return my_update_record( 'alamat' , 'alamat_id' , $id , $datas);
	}
	return my_insert_record('alamat' , $datas );
}

function get_kodepos_id_by_kodepos($kodepos){
	$query = "SELECT kodepos_id FROM kodepos WHERE kodepos ='{$kodepos}'";
	$result = my_query($query);
	if( my_num_rows($result) > 0 ){
		$r = my_fetch_array($result);
		return $r['kodepos_id'];
	}
	
	$datas = array(
			'kodepos'=>my_type_data_str($kodepos)
		);
	$kode_id = my_insert_record('kodepos' , $datas);	
	return $kode_id;
} 